package com.zjw.zy.utils;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

//import com.permissionx.guolindev.PermissionX;
//import com.permissionx.guolindev.callback.ExplainReasonCallback;
//import com.permissionx.guolindev.callback.RequestCallback;
//import com.permissionx.guolindev.request.ExplainScope;

import java.util.List;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/29 11:01
 */
public final class PermissionUtils {

    private static final String TAG = "PermissionUtils";

    /**
     * 动态权限
     */
    public static final String[] sPermissions = new String[]{
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
    };

    public static boolean accessPermission(@NonNull String permission) {
        return ActivityCompat.checkSelfPermission(AppUtils.getApp(), permission) == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean accessPermissions(@NonNull String[] permissions) {
        for (String permission : permissions) {
            if (!accessPermission(permission)) {
                return false;
            }
        }
        return true;
    }

    public static void requestPermissionWhenLaunch() {
        if (!accessPermissions(sPermissions)) {
            ActivityCompat.requestPermissions(AppUtils.getMainActivity(), sPermissions, 1);
        }
    }

    public static void toastNotAccessPermission() {
        LogUtils.e(TAG, "toastNotAccessPermission ");
        ToastUtils.showLong("没有对应权限，请前往设置手动开启");
    }

    /**
     * 以下PermissionX
     */
    private static void permissionX() {
//        PermissionX.init(new Fragment())
//                .permissions()
//                .onExplainRequestReason((scope, deniedList) -> {
//                    //解释一些权限
//                    //scope.showRequestReasonDialog();
//                })
//                .request((allGranted, grantedList, deniedList) -> {
//
//                });
    }
}
